package com.stt.weather.base.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.stt.weather.model.WeatherData;
import com.stt.weather.util.CityUtil;
import org.springframework.stereotype.Service;

/**
 * 天气服务类
 * 负责获取天气数据和发送天气通知
 */
@Service
public class WeatherService {
    
    /**
     * 获取指定城市的天气数据
     * 目前是模拟数据，实际项目中应该调用天气API
     *
     * @param cityCode 城市编号
     * @return 天气数据对象
     */
    public WeatherData getWeatherData(String cityCode) {

        // 实时天气查询
        String key = "b48ff5173c4e49d789d930dbfcab9419";
        String url = "https://devapi.qweather.com/v7/weather/now?location="
                + cityCode + "&key=" + key;
        String response = HttpUtil.createGet(url).execute().body();
        if(StrUtil.isNotEmpty(response)) {
            String code = JSONUtil.parseObj(response).getStr("code");
            if("200".equals(code)) {
                String data = JSONUtil.parseObj(response).getStr("now");
                WeatherData weatherData = JSONUtil.toBean(data, WeatherData.class);
                // 设置城市名称和编码
                weatherData.setCityCode(cityCode);
                weatherData.setCityName(CityUtil.getCityName(cityCode));
                return weatherData;
            }
        }
        return null;
    }

    /**
     * 发送邮件通知
     * 目前只是打印到控制台，实际项目中应该调用邮件服务
     *
     * @param weatherData 天气数据对象
     */
    public void sendEmailNotification(WeatherData weatherData) {
        System.out.println("发送邮件通知：" + weatherData.getCityName() + 
            " 温度：" + weatherData.getTemp() +
            "°C 湿度：" + weatherData.getHumidity() + 
            "% 风速：" + weatherData.getWindSpeed() + "m/s");
    }

    /**
     * 发送短信通知
     * 目前只是打印到控制台，实际项目中应该调用短信服务
     *
     * @param weatherData 天气数据对象
     */
    public void sendSMSNotification(WeatherData weatherData) {
        System.out.println("发送短信通知：" + weatherData.getCityName() + 
            " 温度：" + weatherData.getTemp() +
            "°C 湿度：" + weatherData.getHumidity() + 
            "% 风速：" + weatherData.getWindSpeed() + "m/s");
    }
} 